package com.xci.platform.auth.controller;

import com.xci.platform.annotation.Authorize;
import com.xci.platform.auth.entity.ParamEntity;
import com.xci.platform.auth.service.ParamService;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.JsonGrid;
import com.xci.platform.helper.ExcelHelper;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 系统参数控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/param")
public class ParamController extends AuthBaseController {
    /**
     * 系统参数服务
     */
    @Resource
    protected ParamService paramService;

    //region 页面视图

    /**
     * 参数首页
     */
    @GetMapping
    @Authorize(code = "SystemParam")
    public String index() {
        return "auth/param/index";
    }

    /**
     * 参数新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemParam.Create")
    public String create(ModelMap map) {
        createMark(map);
        ParamEntity entity = new ParamEntity();
        entity.setId(StringHelper.guid());
        map.put("entity", entity);
        return "auth/param/edit";
    }

    /**
     * 参数编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "SystemParam.Edit")
    public String edit(String id, ModelMap map) {
        ParamEntity entity = paramService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/param/edit";
    }

    /**
     * 参数详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        ParamEntity entity = paramService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/param/details";
    }

    //endregion

    //region 数据处理

    /**
     * 参数表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemParam.Search")
    public JsonGrid grid(@RequestParam Map params) {
        return BoolMessage.grid(paramService.queryPage(params));
    }

    /**
     * 保存新增系统参数
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize(code = "SystemParam.Create")
    public BoolMessage createSave(@ModelAttribute ParamEntity entity) {
        return paramService.save(entity, true);
    }

    /**
     * 保存修改系统参数
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize(code = "SystemParam.Edit")
    public BoolMessage editSave(@ModelAttribute ParamEntity entity) {
        return paramService.save(entity, false);
    }

    /**
     * 删除参数
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemParam.Delete")
    public BoolMessage delete(String ids) {
        for (String id : StringHelper.splitToArray(ids)) {
            paramService.delete(id);
        }
        return BoolMessage.True;
    }

    /**
     * 导出参数
     */
    @GetMapping("/export")
    @Authorize(code = "SystemParam.Export")
    public void export(@RequestParam Map params, HttpServletResponse response) throws IOException {
        ExcelHelper.exportWeb(response, paramService.query(params), ParamEntity.class, "系统参数");
    }

    //endregion
}